import java.util.*;
import java.util.regex.Pattern;

public class Setup {
	protected ArrayList<Tile> word;
	protected ArrayList<Tile> rack;
	protected char x;
	protected int y;
	protected Directions dir;
	protected String tilesNeeded;
	protected int score = 0;
	protected String origWord;
	protected ArrayList<String> crosswords; // keeps all possible words to chellange;

	public Setup(String inputString, ArrayList<Tile> rack) {
		this.rack = rack;
		word = new ArrayList<Tile>();
		crosswords = new ArrayList<String>();
		
		// a5 D LambdaCalculus
		Scanner sc = new Scanner(inputString);
		Pattern coordsp = Pattern.compile("^[a-o][1-9][0-5]?",
				Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		String coords = sc.next(coordsp);
		Pattern dirsp = Pattern.compile("[rd]", Pattern.CASE_INSENSITIVE
				| Pattern.UNICODE_CASE);
		String dircode = sc.next(dirsp);
		if (dircode.equals("D")) dir = Directions.DOWN;
		if (dircode.equals("R")) dir = Directions.RIGHT;
		Pattern wordp = Pattern.compile(".*", Pattern.CASE_INSENSITIVE
				| Pattern.UNICODE_CASE);
		origWord = sc.next(wordp);
		x = coords.charAt(0);
		y = Integer.parseInt(coords.substring(1));
		String tilesNeeded = new String(origWord);
		while (tilesNeeded.contains("[")) {
			int lBracketIdx = tilesNeeded.indexOf("[");
			int rBracketIdx = tilesNeeded.indexOf("]");
			tilesNeeded = tilesNeeded.substring(0, lBracketIdx)
					+ tilesNeeded.substring(rBracketIdx + 1);
			origWord = origWord.replaceAll("[\\[\\]]", "");
		}
		crosswords.add(origWord);
		
		boolean found = false;
		for (int i = 0; i < tilesNeeded.length(); i++) {
			char c = tilesNeeded.charAt(i);

			for (Tile each : rack) {
				if (each.getLetter() == c) {
					word.add(each);
					rack.remove(each);
					found = true;
					break;
				} else {
					found = false;
				}

			}
		}
		if (!found)
			returnTiles();
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getScore() {
		return score;
	}

	public ArrayList<Tile> getWord() {
		return word;
	}

	public void setWord(ArrayList<Tile> word) {
		this.word = word;
	}

	public char getX() {
		return x;
	}

	public void setX(char x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Directions getDir() {
		return dir;
	}

	public void setDir(Directions dir) {
		this.dir = dir;
	}

	public String getWordStr() {
		return origWord;
	}

	public void setWordStr(String wordStr) {
		this.tilesNeeded = wordStr;
	}

	public ArrayList<Tile> Tiles() {
		return word;
	}

	protected void returnTiles() {
		for (Tile each1 : word) {
			rack.add(each1);
			// word.remove(each1);
		}
	}

	public void success(boolean s) {
		if (s) {
			Display.displayNotification("Word: " + origWord
					+ " succesfully placed on the board at: " + x + y + " " + dir +" for "
					+ score + " pts.");
			

		} else {
			Display.displayNotification("Could not place word: " + origWord
					+ " at " + x + y + " " + dir);
			returnTiles();

		}
	}
	public void challenged(){
		returnTiles();
		score = 0 - score;
	}
	public boolean canChellange(String s){
		boolean can = false;
		for (String w: crosswords){
			if ( s.equals(w)){
				can = true;
				break;
			}
		}
		return can;
	}
}